<template>
  <div class="article">
    <div class="article_left">
      <div class="article_leftBtn">
        <a href="/User/article/uploadArticle" target="_blank">写文章</a>
        <a href="/topic">看话题</a>
      </div>
      <div class="article_huodong">
        <div class="huodong_title">
          <span>最新活动</span>
          <a href="JavaScript:;">更多</a>
        </div>
        <ul>
          <li>。。。</li>
        </ul>
      </div>
      <article-ranking v-if="timeRanking.length" :rankingData="timeRanking.slice(0,10)" style="margin: 10px 0">
        <svg class='icon' aria-hidden='true'>
          <use xlink:href='#icon-yishoucang'></use>
        </svg>今日文章
      </article-ranking>
      <article-ranking v-if="hostRanking.length" :rankingData="hostRanking.slice(0,10)">
        <svg class='icon' aria-hidden='true'>
          <use xlink:href='#icon-remen'></use>
        </svg>热门文章
      </article-ranking>
    </div>
    <article-right
      @seachArticle="seachArticle" 
      :articleData="articleData"
      @classifyArticle="classifyArticle"
    ></article-right>
  </div>
</template>

<script>
import articleRanking from "./articleRanking.vue"
import articleRight from "./articleRight.vue"
export default {
  name: 'ArticleAll',

  data() {
    return {
      result: [], //所有文章数据集
      hostRanking: [], //热门文章数据集
      articleData: [], //所有展示文章数据集
      timeRanking:[], //今日文章数据集
    };
  },

  mounted() {
    this.init()
  },

  methods: {
    // 初始化
    init(){
      this.$api.getAllArticle().then(res => {
        this.result = res.data
        this.articleData = res.data
        this.hostRanking = res.data.slice(0).sort((a,b) => b.visit - a.visit)
        this.timeRanking = res.data.filter((v) => 
          v.time.split('T')[0] == new Date( +new Date() + 8 * 3600 * 1000 ).toJSON().slice(0,10).replace("T"," "))
      })
    },
    // 检索文章
    seachArticle(val){
      this.articleData = this.result.filter((v) => 
        v.title.indexOf(val) != -1
      )
    },
    // 过滤文章
    classifyArticle(val){
      if(val == '全部文章'){
        this.articleData = this.result
      }else{
        this.articleData = this.result.filter((v) => v.classify == val)
      }
    }
  },

  components: {
   articleRanking,articleRight
  }
};
</script>

<style scoped>
.article{
  width: 100%;
  background-color: #ececec;
  display: flex;
  padding: 20px;
}
.article .article_left{
  width: 240px;
  font-size: 0.7rem;
}
.article .article_left .article_leftBtn{
  width: 100%;
  height: 60px;
  background-color: white;
  display: flex;
  align-items: center;
}
.article_leftBtn a {
  display: inline-block;
  width: 80px;
  height: 30px;
  line-height: 30px;
  border: 1px solid gray;
  text-align: center;
  margin: 0 20px;
}
.article_leftBtn a:hover,.article_leftBtn a:nth-child(1){
  background-color: green;
  color: white;
}
.article_huodong{
  width: 100%;
  background-color: white;
  margin: 10px auto;
}
.article_huodong .huodong_title{
  display: flex;
  width: 100%;
  justify-content: space-between;
  padding: 10px;
  border-bottom: 1px solid rgb(189, 189, 189);
}
.article_huodong .huodong_title a{
  color: green;
}
.article_huodong ul li{
  padding: 10px;
}
.article .article_right{
  width: 100%;
  min-width: 300px;
}
@media screen and (max-width:800px) {
  .article{
    flex-wrap: wrap;
    padding: 0;
    flex-direction: column-reverse;
  }
  .article_right{
    margin: 0;
  }
  .article .article_left .article_leftBtn{
    display: none;
  }
  .article .article_left{
    width: 100%;
  }
  .article_huodong{
    margin: 0;
  }
}
</style>